package com.wangjiyuan.giftshuo.ui.activity.other;

import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.style.UpdateAppearance;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.App;
import com.wangjiyuan.giftshuo.base.BaseActivity;
import com.wangjiyuan.giftshuo.bean.update.VersionBean;
import com.wangjiyuan.giftshuo.http.HttpUtils;
import com.wangjiyuan.giftshuo.service.UpdateService;
import com.wangjiyuan.giftshuo.ui.fragment.classify.mainfragment.ClassifyFragment;
import com.wangjiyuan.giftshuo.ui.fragment.home.NavFragment;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.other.ListTopFragment;
import com.wangjiyuan.giftshuo.ui.fragment.mine.MineFragment;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Context context = this;
    private RadioButton home, listtop, classify, mine;
    private NavFragment mHomeFragment;
    private ClassifyFragment mClassifyFragment;
    private ListTopFragment mListTopFragment;
    private MineFragment mMineFragment;
    long end_time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");

        mHomeFragment = new NavFragment();
        mClassifyFragment = new ClassifyFragment();
        mListTopFragment = new ListTopFragment();
        mMineFragment = new MineFragment();
        mHomeFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_main, mMineFragment, null)
                .add(R.id.activity_main, mClassifyFragment, "ClassifyFragment")
                .add(R.id.activity_main, mListTopFragment, null)
                .add(R.id.activity_main, mHomeFragment, null)
                .commit();

    }

    private void initView() {
        home = (RadioButton) findViewById(R.id.home);
        home.setChecked(true);
        listtop = (RadioButton) findViewById(R.id.listtop);
        classify = (RadioButton) findViewById(R.id.classify);
        mine = (RadioButton) findViewById(R.id.mine);
        home.setOnClickListener(this);
        listtop.setOnClickListener(this);
        classify.setOnClickListener(this);
        mine.setOnClickListener(this);
        UpdateApp();
    }

    private void UpdateApp() {
        try {
            final int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            Observable<VersionBean> versionBeanObservable = HttpUtils.getInstance().getHttpInterfaces().VersionCheck("http://oh0vbg8a6.bkt.clouddn.com/versioncheck.json");
            versionBeanObservable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<VersionBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(final VersionBean versionBean) {
                            int version = versionBean.getData().getVersion();
                            if (version > versionCode) {
                                new AlertDialog.Builder(context)
                                        .setTitle("需要更新")
                                        .setMessage("发现更新！")
                                        .setNegativeButton("是", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                //下载并更新
                                                Intent intent = new Intent(context, UpdateService.class);
                                                intent.putExtra("update_url", versionBean.getData().getVsersionUrl());
                                                startService(intent);
                                            }
                                        })
                                        .setPositiveButton("否", null)
                                        .create()
                                        .show();
                            }
                        }
                    });
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onBackPressed() {

        long start_time = System.currentTimeMillis();
        if (start_time - end_time < 2000) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再按一次退出「礼物说」", Toast.LENGTH_SHORT).show();
            end_time = start_time;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                getSupportFragmentManager().beginTransaction()
                        .show(mHomeFragment)
                        .hide(mClassifyFragment)
                        .hide(mListTopFragment)
                        .hide(mMineFragment)
                        .commit();
                break;
            case R.id.listtop:
                getSupportFragmentManager().beginTransaction()
                        .show(mListTopFragment)
                        .hide(mClassifyFragment)
                        .hide(mHomeFragment)
                        .hide(mMineFragment)
                        .commit();
                break;
            case R.id.classify:
                getSupportFragmentManager().beginTransaction()
                        .show(mClassifyFragment)
                        .hide(mHomeFragment)
                        .hide(mListTopFragment)
                        .hide(mMineFragment)
                        .commit();
                break;
            case R.id.mine:
                getSupportFragmentManager().beginTransaction()
                        .show(mMineFragment)
                        .hide(mHomeFragment)
                        .hide(mClassifyFragment)
                        .hide(mListTopFragment)
                        .commit();
                break;
        }
    }
}
